﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DentalSoftware
{
    public partial class ThemBenhNhan : Form
    {
        private int status;

        private BenhNhanDTO benhnhan;

        public ThemBenhNhan()
        {
            InitializeComponent();
            status = 0;
            txtMaHoSo.Text = BenhNhanBUS.GetMaxMa().ToString();
        }

        public ThemBenhNhan(BenhNhanDTO bn)
        {
            InitializeComponent();
            benhnhan = bn;            
            status = 1;        
        }

        private void ThemBenhNhan_Load(object sender, EventArgs e)
        {
            cbbNgheNghiep.DataSource = ChooseBUS.SelectTenNgheNghiepByAll();
            cbbNgheNghiep.ValueMember = @"Ma";
            cbbNgheNghiep.DisplayMember = @"Ten";
            cbbQuan.DataSource = ChooseBUS.SelectTenQuanByAll();
            cbbQuan.ValueMember = @"Ma";
            cbbQuan.DisplayMember = @"Ten";
            if (status == 1)
            {
                txtMaHoSo.Text = benhnhan.Ma.ToString();
                txtHoTen.Text = benhnhan.Hoten;
                txtHoTen.Text = VietHoaChuCaiDau(txtHoTen.Text);
                txtDienThoai.Text = benhnhan.Dienthoai;
                txtDiaChi.Text = benhnhan.Diachi;
                txtTuoi.Text = (DateTime.Now.Year - int.Parse(benhnhan.Ngaysinh.Year.ToString())).ToString();
                cbbQuan.SelectedValue = benhnhan.Maquan;
                cbbNgheNghiep.SelectedValue = benhnhan.Manghenghiep;
                if (benhnhan.DaChupPhim == 1)
                    ckbDaChupPhim.Checked = true;
                else
                    ckbDaChupPhim.Checked = false;
                if (benhnhan.Gioitinh == 0)
                {
                    rbnNam.Checked = true;
                    rbnNu.Checked = false;
                }
                else
                {
                    rbnNam.Checked = false;
                    rbnNu.Checked = true;
                }
                List<BenhTienSuDTO> dsbts = BenhNhanBUS.SelectBenhNhan_BenhTienSuByMaBN(benhnhan.Ma);
                for (int i = 0; i < dsbts.Count; i++)
                {
                    switch (dsbts[i].MaBTS)
                    {
                        case 1:
                            checkBox1.Checked = true;
                            break;
                        case 2:
                            checkBox2.Checked = true;
                            break;
                        case 3:
                            checkBox3.Checked = true;
                            break;
                        case 4:
                            checkBox4.Checked = true;
                            break;
                        case 5:
                            checkBox5.Checked = true;
                            break;
                        case 6:
                            checkBox6.Checked = true;
                            break;
                        case 7:
                            checkBox7.Checked = true;
                            break;
                        case 8:
                            checkBox8.Checked = true;
                            break;
                        case 9:
                            checkBox9.Checked = true;
                            break;
                        case 10:
                            checkBox10.Checked = true;
                            break;
                        case 11:
                            checkBox11.Checked = true;
                            break;
                    }
                }
            }
        }      

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (txtBenhKhac.Enabled == false)
                txtBenhKhac.Enabled = true;
            else
                txtBenhKhac.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            BenhNhanDTO benhnhan = new BenhNhanDTO();
            benhnhan.Ma = int.Parse(txtMaHoSo.Text);
            benhnhan.Hoten = txtHoTen.Text;
            if (rbnNam.Checked == true)
                benhnhan.Gioitinh = 0;
            else
                benhnhan.Gioitinh = 1;
            benhnhan.Hinhanh = "Chưa có";
            benhnhan.Khamdinhki = 1;
            benhnhan.Maloai = 3;
            benhnhan.Manghenghiep = int.Parse(cbbNgheNghiep.SelectedValue.ToString());
            benhnhan.Maquan = int.Parse(cbbQuan.SelectedValue.ToString());
            benhnhan.Diachi = txtDiaChi.Text;
            benhnhan.Dienthoai = txtDienThoai.Text;
            if (ckbDaChupPhim.Checked)
                benhnhan.DaChupPhim = 1;
            else
                benhnhan.DaChupPhim = 0;
            if (txtTuoi.Text == "")
                txtTuoi.Text = "0";

            if(int.Parse(txtTuoi.Text) > 1000)
                benhnhan.Ngaysinh = new DateTime(int.Parse(txtTuoi.Text), 1, 1);
            else
                benhnhan.Ngaysinh = new DateTime(DateTime.Now.Year - int.Parse(txtTuoi.Text), 1, 1);
            
            benhnhan.DuaTruoc = 0;
            benhnhan.ChiPhi = 0;
            bool kq = true;
            switch (status)
            {
                case 0:
                    {
                        kq = BenhNhanBUS.InsertBenhNhan(benhnhan);
                        break;
                    }
                case 1:
                    {
                        kq = BenhNhanBUS.UpdateBenhNhan(benhnhan);
                        break;
                    }
            }
            if (kq)
            {
                LuuBenhTienSu(benhnhan.Ma);
                //MessageBox.Show("Thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.Yes;                
                this.Close();
            }
            else
            {
                //MessageBox.Show("Thông tin chưa chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void LuuBenhTienSu(int mabn)
        {
            if (checkBox1.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 1, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 1);
            if (checkBox2.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 2, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 2);
            if (checkBox3.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 3, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 3);
            if (checkBox4.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 4, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 4);
            if (checkBox5.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 5, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 5);
            if (checkBox6.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 6, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 6);
            if (checkBox7.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 7, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 7);
            if (checkBox8.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 8, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 8);
            if (checkBox9.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 9, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 9);
            if (checkBox10.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 10, "");
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 10);
            if (checkBox11.Checked)
                BenhNhanBUS.InsertUpdateBenhNhan_BenhTienSu(mabn, 11, txtBenhKhac.Text);
            else
                BenhNhanBUS.DeleteBenhNhan_BenhTienSu(mabn, 11);
        }

        private string VietHoaChuCaiDau(string s)  
        {  
            if (String.IsNullOrEmpty(s))  
                return s;  
  
            string result = "";  
  
            //lấy danh sách các từ  

            string[] words = s.Split(' ');
  
            foreach (string word in words)  
            {  
                // từ nào là các khoảng trắng thừa thì bỏ  
                if (word.Trim() != "")  
                {  
                    if (word.Length > 1)  
                        result += word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + " ";  
                    else  
                        result += word.ToUpper() + " ";  
                }  
  
            }  
            return result.Trim();  
        }

        private void txtHoTen_Leave(object sender, EventArgs e)
        {
            txtHoTen.Text = VietHoaChuCaiDau(txtHoTen.Text);
        }   
    }
}
